package com.ymatou.iapi.productquery.testcase;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymatou.iapi.productquery.service.GetCatalogListByTradeIsolationCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.LiveCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.JsonBeanHelper;
import com.ymttest.utils.MapUtil;

public class Ts_GetCatalogListByTradeIsolation {
	private static GetCatalogListByTradeIsolationCall getCatalogListByTradeIsolationCall = new GetCatalogListByTradeIsolationCall();
	private static YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("交易隔离查询接口, 购物车中商品列表（普通购物车中用）");
	}

	@Before
	public void caseUp() {
		
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getCatalogListByTradeIsolationCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCatalogListByTradeIsolation_001() {
		Logger.start(true,"普通单规格商品，普通单规格直播商品 查询");
		Logger.comment("has been test in Tc_GetCatalogListByCatalogIdList_001");
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCatalogListByTradeIsolation_002() {
		Logger.start(true,"普通多规格商品,普通多规格直播商品 查询");
		Logger.comment("has been test in Tc_GetCatalogListByCatalogIdList_002");
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCatalogListByTradeIsolation_003() {
		Logger.start(true,"普通活动商品查询");
		Logger.comment("has been test in Tc_GetCatalogListByCatalogIdList_003");
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCatalogListByTradeIsolation_004() {
		Logger.start(true,"交易隔离活动商品查询");
		Logger.comment("has been test in Tc_GetCatalogListByCatalogIdList_004");
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCatalogListByTradeIsolation_005() {
		Logger.start(true, "审核中的商品做删除处理");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV2.initAddSellerGlobalPspProductRequest(3383);
			addProdReq.getProduct().setSecondCategoryId(1804);
			String productId = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject()
					.get("ProductId").getAsString();
			Thread.sleep(3000);
			List<String> catalogs = ymtCatalogsIWapper.selectCatalogsByProductId(productId).stream().map(ele->ele.getsCatalogId()).collect(Collectors.toList());
			getCatalogListByTradeIsolationCall.search(catalogs);
			JsonObject product = getCatalogListByTradeIsolationCall.getJsonObject().getAsJsonObject("Data")
					.getAsJsonArray("ProductList").get(0).getAsJsonObject();
			Logger.verifyEquals(-1, product.get("Status").getAsInt(), "审核中的商品做删除处理");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCatalogListByTradeIsolation_006() {
		Logger.start(true, "审核失败商品做删除处理");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV2.initAddSellerGlobalPspProductRequest(3383);
			addProdReq.getProduct().setSecondCategoryId(1804);
			String productId = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject()
					.get("ProductId").getAsString();
			Thread.sleep(3000);
			ProductManagerCallServiceV3.UpdateAuditStatus(productId, -1);
			Thread.sleep(3000);
			List<String> catalogs = ymtCatalogsIWapper.selectCatalogsByProductId(productId).stream().map(ele->ele.getsCatalogId()).collect(Collectors.toList());
			getCatalogListByTradeIsolationCall.search(catalogs);
			JsonObject product = getCatalogListByTradeIsolationCall.getJsonObject().getAsJsonObject("Data").getAsJsonArray("ProductList").get(0).getAsJsonObject();
			Logger.verifyEquals(-1,product.get("Status").getAsInt(),"审核失败商品做删除处理");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCatalogListByTradeIsolation_007() {
		Logger.start(true, "审核通过商品不做删除处理");
		try {
			AddSellerProductRequest addProdReq = ProductManagerCallServiceV2.initAddSellerGlobalPspProductRequest(3383);
			addProdReq.getProduct().setSecondCategoryId(1804);
			String productId = ProductManagerCallServiceV2.addNewProduct(addProdReq).get("Data").getAsJsonObject()
					.get("ProductId").getAsString();
			Thread.sleep(3000);
			ProductManagerCallServiceV3.UpdateAuditStatus(productId, 0);
			Thread.sleep(3000);
			List<String> catalogs = ymtCatalogsIWapper.selectCatalogsByProductId(productId).stream().map(ele->ele.getsCatalogId()).collect(Collectors.toList());
			getCatalogListByTradeIsolationCall.search(catalogs);
			JsonObject product = getCatalogListByTradeIsolationCall.getJsonObject().getAsJsonObject("Data")
					.getAsJsonArray("ProductList").get(0).getAsJsonObject();
			Logger.verifyEquals(0, product.get("Status").getAsInt(), "审核通过的商品不做删除处理");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCatalogListByTradeIsolation_008() {
		Logger.start(true, "审核通过商品不做删除处理");
		try {
			String productId="p4473312";
			List<String> catalogs = ymtCatalogsIWapper.selectCatalogsByProductId(productId).stream().map(ele->ele.getsCatalogId()).collect(Collectors.toList());
			getCatalogListByTradeIsolationCall.search(catalogs);
			JsonObject product = getCatalogListByTradeIsolationCall.getJsonObject().getAsJsonObject("Data")
					.getAsJsonArray("ProductList").get(0).getAsJsonObject();
			Logger.verifyEquals(0, product.get("Status").getAsInt(), "审核通过的商品不做删除处理");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
}
